<?php
/**
 * 初始化数据库数据脚本
 * 运行此脚本确保有基础的测试数据
 */

require_once __DIR__ . '/vendor/autoload.php';

use think\facade\Db;

try {
    // 插入部门测试数据
    $departments = [
        ['name' => '技术部', 'description' => '负责软件开发和技术支持'],
        ['name' => '人事部', 'description' => '负责人力资源管理'],
        ['name' => '市场部', 'description' => '负责市场推广和销售'],
        ['name' => '财务部', 'description' => '负责财务管理和会计'],
        ['name' => '行政部', 'description' => '负责行政事务管理']
    ];
    
    echo "开始插入部门数据...\n";
    foreach ($departments as $dept) {
        $existing = Db::name('department')->where('name', $dept['name'])->find();
        if (!$existing) {
            Db::name('department')->insert([
                'name' => $dept['name'],
                'description' => $dept['description'],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ]);
            echo "插入部门: {$dept['name']}\n";
        } else {
            echo "部门已存在: {$dept['name']}\n";
        }
    }
    
    // 获取部门ID用于插入岗位
    $techDept = Db::name('department')->where('name', '技术部')->find();
    $hrDept = Db::name('department')->where('name', '人事部')->find();
    $marketDept = Db::name('department')->where('name', '市场部')->find();
    $financeDept = Db::name('department')->where('name', '财务部')->find();
    $adminDept = Db::name('department')->where('name', '行政部')->find();
    
    // 插入岗位测试数据
    $positions = [
        ['name' => '前端开发工程师', 'department_id' => $techDept['id'], 'description' => '负责前端页面开发'],
        ['name' => '后端开发工程师', 'department_id' => $techDept['id'], 'description' => '负责后端接口开发'],
        ['name' => '产品经理', 'department_id' => $techDept['id'], 'description' => '负责产品规划设计'],
        ['name' => '招聘专员', 'department_id' => $hrDept['id'], 'description' => '负责人员招聘工作'],
        ['name' => '培训专员', 'department_id' => $hrDept['id'], 'description' => '负责员工培训工作'],
        ['name' => '市场推广专员', 'department_id' => $marketDept['id'], 'description' => '负责市场推广工作'],
        ['name' => '销售经理', 'department_id' => $marketDept['id'], 'description' => '负责销售管理工作'],
        ['name' => '会计', 'department_id' => $financeDept['id'], 'description' => '负责财务核算工作'],
        ['name' => '出纳', 'department_id' => $financeDept['id'], 'description' => '负责资金管理工作'],
        ['name' => '行政助理', 'department_id' => $adminDept['id'], 'description' => '负责行政辅助工作']
    ];
    
    echo "开始插入岗位数据...\n";
    foreach ($positions as $pos) {
        $existing = Db::name('position')->where('name', $pos['name'])->where('department_id', $pos['department_id'])->find();
        if (!$existing) {
            Db::name('position')->insert([
                'name' => $pos['name'],
                'department_id' => $pos['department_id'],
                'description' => $pos['description'],
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ]);
            echo "插入岗位: {$pos['name']}\n";
        } else {
            echo "岗位已存在: {$pos['name']}\n";
        }
    }
    
    echo "数据初始化完成！\n";
    
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}